<?php
//
// test HTTP POST submitter, using libcurl
//
include_once "config.php";
include_once "common.php";

$auth =  $_COOKIE[$th_auth];
$username = $_COOKIE[$th_username];

if ($auth && !empty($auth) && $username && !empty($username)){
//if already log in, skip	




}

else {
//do login from login form
//login($_POST["username"],$_POST["password"]);

login($_POST["username"],$_POST["password"]);

}

$post_data = array ( 'auth' => ($auth) );

$progress = 'http://api.bgm.tv/user/'.$username.'/collection?cat=watching&source='.$source;

$result = curl_post($progress, $post_data);

print $result;





function login($local_username,$local_password){
	global $auth, $username, $api_auth, $source,$th_username,$th_id,$th_auth, $th_avatar, $th_nickname ;
	
	$login = $api_auth."?source=".$source;
	
	$confidential  = array('username' => $local_username, 'password' => $local_password);

	
	$result = curl_post($login, $confidential);
	
	$json = json_decode($result);
	
	$code = $json -> {'code'};
	
	if ($code && !empty($code)){
		return;
	}
	else{
		if ( $json -> {'username'} && $json -> {'id'} && $json -> {'auth'}){
			setcookie($th_username, $json -> {'username'}, time()+360000); 
			setcookie($th_id, $json -> {'id'}, time()+360000);
			setcookie($th_auth, $json -> {'auth'}, time()+360000);
			setcookie($th_avatar, $json -> {'avatar'} -> {'large'}, time()+360000);

		}
		
	}
	
	$auth = $json -> {'auth'} ;
	$username = $json -> {'username'};

}




?>